﻿<!-- 
Project: ClipFlair (http://ClipFlair.codeplex.com)
Filename: Generic.xaml
Version: 20150323
-->
  
<!-- 
  TODO: re-template the DataGrid to turn off row virtualization, since it causes issue with audio playback (stops playing audio for rows that get out of the view [it destroys them] if you scroll up/down)
        see how to do it at http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/13/performance-characteristics-of-the-silverlight-datagrid.aspx
-->
  
<ResourceDictionary
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
 xmlns:audio="clr-namespace:ClipFlair.AudioRecorder;assembly=AudioRecorder"
 xmlns:conv="clr-namespace:Utils.Converters;assembly=Utils"
 xmlns:bind="clr-namespace:Utils.Bindings;assembly=Utils"
 xmlns:res="clr-namespace:ClipFlair.CaptionsGrid.Resources"
 xmlns:captions="clr-namespace:ClipFlair.CaptionsGrid"
 > <!-- xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit" -->

  <res:CaptionsGridStrings x:Key="localizedStrings"/>
  
  <conv:BooleanToFlowDirectionConverter x:Key="BooleanToFlowDirectionConverter" />
  <conv:ArrayToStringConverter x:Key="ArrayToStringConverter" />
 
  <!-- Toolbar -->

  <SolidColorBrush x:Key="ToolbarBackground" Color="#E3DAC9" />

  <!-- Index -->

  <DataTemplate x:Key="IndexCellTemplate">
    <TextBlock Margin="4" Text="{Binding Index}" />
  </DataTemplate>

  <!-- StartTime -->
  
  <DataTemplate x:Key="StartTimeCellTemplate">
    <TextBlock Margin="4" Text="{Binding Begin, StringFormat='hh\\:mm\\:ss'}" />
    <!-- can use .fff to always show all digits, don't use .fFF - has bug at parsing new values -->
  </DataTemplate>

  <DataTemplate x:Key="StartTimeCellTemplate_msec">
    <TextBlock Margin="4" Text="{Binding Begin, StringFormat='hh\\:mm\\:ss\\.FFF'}" />
    <!-- can use .fff to always show all digits, don't use .fFF - has bug at parsing new values -->
  </DataTemplate>

  <DataTemplate x:Key="StartTimeCellEditTemplate">
    <TextBox Text="{Binding Begin, StringFormat='hh\\:mm\\:ss\\.FFF', Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
    <!--<input:TimeUpDown Format="hh:mm:ss" Value="{Binding Begin, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />-->
  </DataTemplate>

  <!-- EndTime -->

  <DataTemplate x:Key="EndTimeCellTemplate">
    <TextBlock Margin="4" Text="{Binding End, StringFormat='hh\\:mm\\:ss'}" />
  </DataTemplate>

  <DataTemplate x:Key="EndTimeCellTemplate_msec">
    <TextBlock Margin="4" Text="{Binding End, StringFormat='hh\\:mm\\:ss\\.FFF'}" />
  </DataTemplate>

  <DataTemplate x:Key="EndTimeCellEditTemplate">
    <TextBox Text="{Binding End, StringFormat='hh\\:mm\\:ss\\.FFF', Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
    <!--<input:TimeUpDown Format="hh:mm:ss" Minimum="{Binding Begin}" Value="{Binding End, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />-->
  </DataTemplate>

  <!-- Duration -->

  <DataTemplate x:Key="DurationCellTemplate">
    <TextBlock Margin="4" Text="{Binding Duration, StringFormat='hh\\:mm\\:ss'}" />
  </DataTemplate>

  <DataTemplate x:Key="DurationCellTemplate_msec">
    <TextBlock Margin="4" Text="{Binding Duration, StringFormat='hh\\:mm\\:ss\\.FFF'}" />
  </DataTemplate>

  <DataTemplate x:Key="DurationCellEditTemplate">
    <TextBox Text="{Binding Duration, StringFormat='hh\\:mm\\:ss\\.FFF', Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
    <!--<input:TimeUpDown Format="hh:mm:ss" Value="{Binding Duration, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />-->
  </DataTemplate>

  <!-- Role -->

  <DataTemplate x:Key="RoleCellTemplate">
    <TextBlock Margin="4" Text="{Binding Role}" />
  </DataTemplate>

  <DataTemplate x:Key="RoleCellEditTemplate">
    <!-- <TextBox Text="{Binding Role, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> -->
    <sdk:AutoCompleteBox
      Text="{Binding Role, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" 
      ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=captions:CaptionsGrid}, Path=Roles}" 
      IsTextCompletionEnabled="True"
      MinimumPrefixLength="0" 
      MinimumPopulateDelay="0"
      />
      <!--
      Style="{StaticResource EditableComboStyle}"                                 
      -->
  </DataTemplate>

  <!-- Caption -->
  
  <DataTemplate x:Key="CaptionCellTemplate">
    <TextBlock
      Margin="4"
      Text="{Binding Caption}"
      FlowDirection="{Binding RTL, Converter={StaticResource BooleanToFlowDirectionConverter}}"
      />
  </DataTemplate>

  <DataTemplate x:Key="CaptionCellEditTemplate">
    <TextBox
      AcceptsReturn="True"
      Text="{Binding Caption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
      FlowDirection="{Binding RTL, Converter={StaticResource BooleanToFlowDirectionConverter}}"
      bind:ImmediateSourceUpdate.IsEnabled="True"
      bind:ImmediateSourceUpdate.Source="{Binding Caption, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}"
      />
      <!-- ImmediateSourceUpdate is used because of http://social.msdn.microsoft.com/Forums/silverlight/en-US/951bde54-1bd1-4fdc-8148-86d5a5183de8/updatesourcetriggerpropertychanged-is-ignored?forum=silverlightgen&prof=required -->
  </DataTemplate>

  <!-- RTL -->

  <DataTemplate x:Key="RTLCellTemplate">
    <CheckBox Margin="4" IsChecked= "{Binding RTL, Mode=TwoWay}" />
  </DataTemplate>

  <!-- CPL -->

  <DataTemplate x:Key="CPLCellTemplate">
    <TextBlock Margin="4" Text="{Binding CPL, Converter={StaticResource ArrayToStringConverter}}" />
  </DataTemplate>
  
  <!-- CPS -->

  <DataTemplate x:Key="CPSCellTemplate">
    <TextBlock Margin="4" Text="{Binding CPS, StringFormat='F2'}" />
  </DataTemplate>
  
  <!-- WPM -->

  <DataTemplate x:Key="WPMCellTemplate">
    <TextBlock Margin="4" Text="{Binding WPM, StringFormat='F2'}" />
  </DataTemplate>

  <!-- Audio -->
  
  <DataTemplate x:Key="AudioCellTemplate">
    <audio:AudioRecorderControl
      Margin="2" 
      Audio="{Binding RelativeSource={RelativeSource AncestorType=FrameworkElement}, Path=DataContext.Audio, Mode=TwoWay}"
      LimitPlayback="{Binding RelativeSource={RelativeSource AncestorType=captions:CaptionsGrid}, Path=LimitAudioPlayback, Mode=TwoWay}"
      LimitRecording="{Binding RelativeSource={RelativeSource AncestorType=captions:CaptionsGrid}, Path=LimitAudioRecording, Mode=TwoWay}"
      MaxPlaybackDuration="{Binding RelativeSource={RelativeSource AncestorType=FrameworkElement}, Path=DataContext.Duration, Mode=TwoWay}"
      MaxRecordingDuration="{Binding RelativeSource={RelativeSource AncestorType=FrameworkElement}, Path=DataContext.Duration, Mode=TwoWay}"
      DrawDuration="{Binding RelativeSource={RelativeSource AncestorType=captions:CaptionsGrid}, Path=DrawAudioDuration, Mode=TwoWay}"
      >
    </audio:AudioRecorderControl>
    <!-- AudioRecorderControl sets its DataContext to its ViewModel, so it doesn't inherit its parent's DataContext -->
  </DataTemplate> <!-- TODO: checkout why TwoWay connection mode is needed for MaxPlaybackDuration/MaxRecordingDuration (OneWay fails to update the respective AudioRecorderControl property), PROBABLY RELATED TO HOW DATACONTEXT IS USED -->

  <!-- Comments -->

  <DataTemplate x:Key="CommentsCellTemplate">
    <TextBlock Margin="4" Text="{Binding Comments}" />
  </DataTemplate>

  <DataTemplate x:Key="CommentsCellEditTemplate">
    <TextBox AcceptsReturn="True" Text="{Binding Comments, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
  </DataTemplate>

  <!-- CommentsAudio -->
  
  <DataTemplate x:Key="CommentsAudioCellTemplate">
    <audio:AudioRecorderControl
      Margin="2"
      Audio="{Binding RelativeSource={RelativeSource AncestorType=FrameworkElement}, Path=DataContext.CommentsAudio, Mode=TwoWay}"
      LimitPlayback="False"
      LimitRecording="False"
      DrawDuration="False"
      /> <!-- AudioRecorderControl sets its DataContext to its ViewModel, so it doesn't inherit its parent's DataContext -->
  </DataTemplate>
  
  <!-- Column Header -->

  <Style x:Key="ColumnHeaderStyle" TargetType="sdk:DataGridColumnHeader" >
    <Setter Property="Foreground" Value="#858585" />
    <Setter Property="Background" Value="#FFFFFF" />
    <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    <!--         
          <Setter Property="ContentTemplate">
            <Setter.Value>
              <DataTemplate>
                <StackPanel 
                  HorizontalAlignment="Stretch">
                  <TextBlock Text="{Binding}" />
                </StackPanel>
              </DataTemplate>
            </Setter.Value>
          </Setter>
    -->
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="sdk:DataGridColumnHeader">
          <Grid Name="Root">
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="SortStates" >
                <VisualStateGroup.Transitions>
                  <VisualTransition GeneratedDuration="00:00:0.1" />
                </VisualStateGroup.Transitions>
                <VisualState x:Name="Unsorted" />
                <VisualState x:Name="SortAscending">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="SortDescending">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="SortIcon" Storyboard.TargetProperty="Opacity" Duration="0" To="1.0" />
                    <DoubleAnimation Storyboard.TargetName="SortIconTransform" Storyboard.TargetProperty="ScaleY" Duration="0" To="-.9" />
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Grid.RowDefinitions>
              <RowDefinition Height="*" />
              <RowDefinition Height="*" />
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Rectangle x:Name="BackgroundRectangle" Stretch="Fill"
                             Fill="{TemplateBinding Background}"
                             Grid.ColumnSpan="2" Grid.RowSpan="2"  />
            <ContentPresenter Grid.RowSpan="2" Content="{TemplateBinding Content}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="{TemplateBinding Padding}" />
            <Rectangle Name="VerticalSeparator" Grid.RowSpan="2" Grid.Column="2" Width="1" VerticalAlignment="Stretch" Fill="{TemplateBinding SeparatorBrush}" Visibility="{TemplateBinding SeparatorVisibility}" />
            <Path Grid.RowSpan="2" Name="SortIcon" RenderTransformOrigin=".5,.5" HorizontalAlignment="Left" VerticalAlignment="Center" Opacity="0" Grid.Column="1" Stretch="Uniform" Width="8" Data="F1 M -5.215,6.099L 5.215,6.099L 0,0L -5.215,6.099 Z ">
              <Path.Fill>
                <SolidColorBrush Color="#FF444444" />
              </Path.Fill>
              <Path.RenderTransform>
                <TransformGroup>
                  <ScaleTransform x:Name="SortIconTransform" ScaleX=".9" ScaleY=".9"  />
                </TransformGroup>
              </Path.RenderTransform>
            </Path>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>

  </Style>

  <!-- EditableCombo --> 
  <!-- from: http://timheuer.com/blog/archive/2008/11/05/silverlight-editable-combobox-using-styles.aspx -->
<!--
  <Style x:Name="ComboToggleButton" TargetType="ToggleButton">
    <Setter Property="Foreground" Value="#FF333333"/>
    <Setter Property="Background" Value="#FF1F3B53"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ToggleButton">
          <Grid>
            <Rectangle Fill="Transparent" />
            <ContentPresenter
                            x:Name="contentPresenter"
                            Content="{TemplateBinding Content}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            Margin="{TemplateBinding Padding}"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  
  <Style x:Key="EditableComboStyle" TargetType="sdk:AutoCompleteBox">
    <Setter Property="FilterMode" Value="StartsWith" />
    <Setter Property="Background" Value="#FF1F3B53"/>
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="BorderBrush">
      <Setter.Value>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
          <GradientStop Color="#FFA3AEB9" Offset="0"/>
          <GradientStop Color="#FF8399A9" Offset="0.375"/>
          <GradientStop Color="#FF718597" Offset="0.375"/>
          <GradientStop Color="#FF617584" Offset="1"/>
        </LinearGradientBrush>
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="sdk:AutoCompleteBox">
          <Grid Margin="{TemplateBinding Padding}">
            <VisualStateManager.VisualStateGroups>
              <VisualStateGroup x:Name="PopupStates">
                <VisualStateGroup.Transitions>
                  <VisualTransition GeneratedDuration="0:0:0.1" To="PopupOpened" />
                  <VisualTransition GeneratedDuration="0:0:0.2" To="PopupClosed" />
                </VisualStateGroup.Transitions>
                <VisualState x:Name="PopupOpened">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="1.0" />
                  </Storyboard>
                </VisualState>
                <VisualState x:Name="PopupClosed">
                  <Storyboard>
                    <DoubleAnimation Storyboard.TargetName="PopupBorder" Storyboard.TargetProperty="Opacity" To="0.0" />
                  </Storyboard>
                </VisualState>
              </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <TextBox IsTabStop="True" x:Name="Text" Style="{TemplateBinding TextBoxStyle}" Margin="0" />
            <ToggleButton 
                                x:Name="DropDownToggle" 
                                HorizontalAlignment="Right"
                                VerticalAlignment="Center"
                                Style="{StaticResource ComboToggleButton}"
                                Margin="0"
                                HorizontalContentAlignment="Center" 
                                Background="{TemplateBinding Background}" 
                                BorderThickness="0" 
                                Height="16" Width="16"
                                >
              <ToggleButton.Content>
                <Path x:Name="BtnArrow" Height="4" Width="8" Stretch="Uniform" Data="F1 M 301.14,-189.041L 311.57,-189.041L 306.355,-182.942L 301.14,-189.041 Z " 
                                          Margin="0,0,6,0" HorizontalAlignment="Right">
                  <Path.Fill>
                    <SolidColorBrush x:Name="BtnArrowColor" Color="#FF333333"/>
                  </Path.Fill>
                </Path>
              </ToggleButton.Content>
            </ToggleButton>
            <Popup x:Name="Popup">
              <Border x:Name="PopupBorder" HorizontalAlignment="Stretch" Opacity="0.0" BorderThickness="0" CornerRadius="3">
                <Border.RenderTransform>
                  <TranslateTransform X="2" Y="2" />
                </Border.RenderTransform>
                <Border.Background>
                  <SolidColorBrush Color="#11000000" />
                </Border.Background>
                <Border HorizontalAlignment="Stretch" BorderThickness="0" CornerRadius="3">
                  <Border.Background>
                    <SolidColorBrush Color="#11000000" />
                  </Border.Background>
                  <Border.RenderTransform>
                    <TransformGroup>
                      <ScaleTransform />
                      <SkewTransform />
                      <RotateTransform />
                      <TranslateTransform X="-1" Y="-1" />
                    </TransformGroup>
                  </Border.RenderTransform>
                  <Border HorizontalAlignment="Stretch" Opacity="1.0" Padding="2" BorderThickness="2" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3">
                    <Border.RenderTransform>
                      <TransformGroup>
                        <ScaleTransform />
                        <SkewTransform />
                        <RotateTransform />
                        <TranslateTransform X="-2" Y="-2" />
                      </TransformGroup>
                    </Border.RenderTransform>
                    <Border.Background>
                      <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFDDDDDD" Offset="0"/>
                        <GradientStop Color="#AADDDDDD" Offset="1"/>
                      </LinearGradientBrush>
                    </Border.Background>
                    <ListBox x:Name="SelectionAdapter" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemTemplate="{TemplateBinding ItemTemplate}" />
                  </Border>
                </Border>
              </Border>
            </Popup>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
-->
</ResourceDictionary>
